package com.ltv.xiangqiplugin.views;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.ltv.xiangqiplugin.models.BoardSizeEnum;
import com.ltv.xiangqiplugin.models.Languages;
import com.ltv.xiangqiplugin.models.Messages;
import com.ltv.xiangqiplugin.models.MessagesObserver;
import com.ltv.xiangqiplugin.models.Themes;

public class OptionView extends JDialog implements MessagesObserver {
	private JComboBox langCombobox;
	private JComboBox boardSizeCombobox;
	private JComboBox themeCombobox;
	
	private JButton ok;
	private JButton cancel;
	
	private JLabel langLbl;
	private JLabel boardSizeLbl;
	private JLabel themeLbl;
	
	private ChessApp mainApp;
	
	public OptionView(ChessApp mainApp) {
		this.mainApp = mainApp;
		String langs[] = new String[] {
				Messages.getInstance().getProperty("english"),
				Messages.getInstance().getProperty("vietnamese")
		};
		langCombobox = new JComboBox(langs);
		
		String sizes[] = new String[] {
				Messages.getInstance().getProperty("boardsize.small"),
				Messages.getInstance().getProperty("boardsize.medium"),
				Messages.getInstance().getProperty("boardsize.large")
		};
		boardSizeCombobox = new JComboBox(sizes);
		
		String themes[] = new String[] {
				Integer.toString(Themes.THEME_1.getValue()),
				Integer.toString(Themes.THEME_2.getValue())
		};
		themeCombobox = new JComboBox(themes);
		
		ok = new JButton(Messages.getInstance().getProperty("button.ok"));
		cancel = new JButton(Messages.getInstance().getProperty("button.cancel"));
		
		langLbl = new JLabel(Messages.getInstance().getProperty("language") + "    ");
		boardSizeLbl = new JLabel(Messages.getInstance().getProperty("boardsize") + "    ");
		themeLbl = new JLabel(Messages.getInstance().getProperty("theme") + "    ");
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new GridBagLayout());
		
		JPanel btns = new JPanel();
		btns.add(ok);
		btns.add(cancel);
		
		GridBagConstraints constrs = new GridBagConstraints();
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 0;
		mainPane.add(langLbl, constrs);
		
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 1;
	    constrs.gridy = 0;
		mainPane.add(langCombobox, constrs);
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 1;
		mainPane.add(new JPanel(), constrs);
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 2;
		mainPane.add(boardSizeLbl, constrs);
		
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 1;
	    constrs.gridy = 2;
		mainPane.add(boardSizeCombobox, constrs);
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 3;
		mainPane.add(new JPanel(), constrs);
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 4;
		mainPane.add(themeLbl, constrs);
		
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 1;
	    constrs.gridy = 4;
		mainPane.add(themeCombobox, constrs);
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 5;
		mainPane.add(new JPanel(), constrs);
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 6;
		mainPane.add(new JPanel(), constrs);
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 7;
		mainPane.add(new JPanel(), constrs);
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridx = 0;
	    constrs.gridy = 8;
	    constrs.gridwidth = 2;
	    mainPane.add(btns, constrs);
	    
	    ok.setActionCommand("OK");
	    cancel.setActionCommand("CANCEL");
	    ok.addActionListener(mainApp.getOptionControl());
	    cancel.addActionListener(mainApp.getOptionControl());
	    
		setContentPane(mainPane);
		setResizable(false);
		centerScreen();
		pack();
		
	}

	@Override
	public void messagesChanged() {
		ok.setText(Messages.getInstance().getProperty("button.ok"));
		cancel.setText(Messages.getInstance().getProperty("button.cancel"));
		langLbl.setText(Messages.getInstance().getProperty("language") + "    ");
		boardSizeLbl.setText(Messages.getInstance().getProperty("boardsize") + "    ");
		themeLbl.setText(Messages.getInstance().getProperty("theme") + "    ");
		
		langCombobox.removeAllItems();
		langCombobox.addItem(Messages.getInstance().getProperty("english"));
		langCombobox.addItem(Messages.getInstance().getProperty("vietnamese"));
		
		boardSizeCombobox.removeAllItems();
		boardSizeCombobox.addItem(Messages.getInstance().getProperty("boardsize.small"));
		boardSizeCombobox.addItem(Messages.getInstance().getProperty("boardsize.medium"));
		boardSizeCombobox.addItem(Messages.getInstance().getProperty("boardsize.large"));
		
		themeCombobox.removeAllItems();
		themeCombobox.addItem(Integer.toString(Themes.THEME_1.getValue()));
		themeCombobox.addItem(Integer.toString(Themes.THEME_2.getValue()));
		
		correctSelectedLanguageIndex();
		correctSelectedBoardSizeIndex();
		correctSelectedThemeIndex();
		
		pack();
	}
	
	public void correctSelectedLanguageIndex() {
		if (Messages.getInstance().getLang() == Languages.EN) {
			langCombobox.setSelectedIndex(0);
		} else if (Messages.getInstance().getLang() == Languages.VI) {
			langCombobox.setSelectedIndex(1);
		}
	}
	
	public void correctSelectedBoardSizeIndex() {
		if (Messages.getInstance().getBoardSize() == BoardSizeEnum.SMALL) {
			boardSizeCombobox.setSelectedIndex(0);
		} else if (Messages.getInstance().getBoardSize() == BoardSizeEnum.MEDIUM) {
			boardSizeCombobox.setSelectedIndex(1);
		} else if (Messages.getInstance().getBoardSize() == BoardSizeEnum.LARGE) {
			boardSizeCombobox.setSelectedIndex(2);
		}
	}
	
	public void correctSelectedThemeIndex() {
		if (Messages.getInstance().getTheme() == Themes.THEME_1) {
			themeCombobox.setSelectedIndex(0);
		} else if (Messages.getInstance().getTheme() == Themes.THEME_2) {
			themeCombobox.setSelectedIndex(1);
		}
	}
	
	public Languages getLanguage() {
		int langIndex = langCombobox.getSelectedIndex();
		
		if (langIndex == 0) {
			return Languages.EN;
		} else if (langIndex == 1) {
			return Languages.VI;
		}
		
		return null;
	}
	
	public BoardSizeEnum getBoardSize() {
		int langIndex = boardSizeCombobox.getSelectedIndex();
		
		if (langIndex == 0) {
			return BoardSizeEnum.SMALL;
		} else if (langIndex == 1) {
			return BoardSizeEnum.MEDIUM;
		} else if (langIndex == 2) {
			return BoardSizeEnum.LARGE;
		}
		
		return null;
	}
	
	public Themes getTheme() {
		int themeIndex = themeCombobox.getSelectedIndex();
		
		if (themeIndex == 0) {
			return Themes.THEME_1;
		} else if (themeIndex == 1) {
			return Themes.THEME_2;
		}
		
		return null;
	}
	
	private void centerScreen() {
		int x = (Toolkit.getDefaultToolkit().getScreenSize().width - getWidth())/2;
		int y = (Toolkit.getDefaultToolkit().getScreenSize().height - getHeight())/2;
		
		setLocation(x, y);
	}

}
